# write in 2019,6,20
#write by lidabao

from exts import db
import shortuuid
from datetime import datetime
from werkzeug.security import check_password_hash,generate_password_hash
import enum

class Gender(enum.Enum):
    male=1
    female=2
    secret=3
    unkown=4

class FrontUser(db.Model):
    id=db.Column(db.String(100),default=shortuuid.uuid,primary_key=True)
    telephone=db.Column(db.String(11),nullable=False,unique=True)
    email=db.Column(db.String(50))
    _password=db.Column(db.String(256),nullable=False)
    realname=db.Column(db.String(20))
    avatar=db.Column(db.String(100))
    username=db.Column(db.String(100))
    signatrue=db.Column(db.String(50))
    gender=db.Column(db.Enum(Gender),default=Gender.unkown)
    join_time=db.Column(db.DateTime,default=datetime.now)

    def __init__(self,*args,**kwargs):
        if "password" in kwargs:
            self.password=kwargs['password']
            kwargs.pop('password')
        super(FrontUser,self).__init__(*args,**kwargs)

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, raw_password):
        self._password = generate_password_hash(raw_password)
        # print(self._password)

    def check_password(self, raw_password):
        if check_password_hash(self.password, raw_password):
            return True
        else:
            return False
